/*
 * VectorUtility.cpp
 *
 *      Author: Jerome RD Soine and Christoph A Brand
 *      Institution: Schwarz goup, Institute for Theoretical Physics, Heidelberg University, Germany
 */

#include "../../../include/core/utility/VectorOperations.h"
#include <cmath>
#include <cstdio>
#include <iostream>

void VectorOperations::data_out_std_vector(std::ofstream& data, std::vector<double> & v)
{
	for(unsigned int i=0;i<v.size();i++)
	{
		data << v[i] << "\t";
	}
	data << "\n";
}

void VectorOperations::cout_std_vector(std::vector<double>& v){
	for(unsigned int i=0; i<v.size(); i++){
		printf("%.16f",v[i]);
		if(i<v.size()-1) std::cout << " - ";
		else std::cout << std::endl;
	}
}

void VectorOperations::add_to_std_vector(double s, std::vector<double> n, std::vector<double>& v)
{
	for(unsigned int i=0;i<v.size();i++)
		v[i] += s*n[i];
}
void VectorOperations::subtract_from_std_vector(double s, std::vector<double> n, std::vector<double>& v)
{
	for(unsigned int i=0;i<v.size();i++)
		v[i] -= s*n[i];
}

void VectorOperations::normalize_vector(std::vector<double>& v)
{
	double norm = 0;
	for(unsigned int i=0;i<v.size();i++)
		norm += v[i]*v[i];
	norm = 1/sqrt((long double)norm);

	for(unsigned int i=0;i<v.size();i++)
		v[i]=norm*v[i];
}

double VectorOperations::max_component_difference(std::vector<double> v1, std::vector<double> v2)
{
	double max_value = abs(v1[0]-v2[0]);
	double tmp_value;
	for(unsigned int i=1;i<v1.size();i++)
	{
		tmp_value = abs(v1[i]-v2[i]);
		if(tmp_value>max_value)
			max_value = tmp_value;
	}
	return max_value;
}

void VectorOperations::scale_std_vector(double s,std::vector<double>& v)
{
	for(unsigned int i=0;i<v.size();i++)
		v[i]=s*v[i];
}

double VectorOperations::scalar_product(std::vector<double> v1,std::vector<double> v2)
{
	double tmp = 0;
	for(unsigned int i=0;i<v1.size();i++)
		tmp += v1[i]*v2[i];
	return tmp;
}

void VectorOperations::change_sign_of_std_vector(std::vector<double> & v){
	for(unsigned int i=0; i<v.size(); i++){
		v[i] = -v[i];
	}
}

double VectorOperations::get_L2_of_std_vector(std::vector<double> v)
{
	double tmp = 0;
	for(unsigned int i=0;i<v.size();i++)
		tmp += v[i]*v[i];
	tmp = std::sqrt((long double) tmp);
	return tmp;
}

double VectorOperations::get_max_component(std::vector<double> & v)
{
	double tmp = v[0];
	for (unsigned int i = 0; i < v.size(); ++i) {
		if(v[i]>tmp)
			tmp = v[i];
	}
	return tmp;
}

double VectorOperations::get_min_component(std::vector<double> & v)
{
	double tmp = v[0];
	for (unsigned int i = 0; i < v.size(); ++i) {
		if(v[i]<tmp)
			tmp = v[i];
	}
	return tmp;
}
